package sample.ui

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.widget.NestedScrollView
import com.netease.core.R
import zx.tas.model.lvVp30
import kotlinx.coroutines.script.C
import kotlinx.coroutines.script.tes.appBuildTime
import kotlinx.coroutines.script.tes.getColor
import kotlinx.coroutines.script.tes.isDebug
import kotlinx.coroutines.script.tes.phoneScreenWidth
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.setOnClickCallback
import kotlinx.coroutines.script.tes.toast

class SetupTaskDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {
    class Builder(private val context: Context) {
        private val canCancel = true
        fun create(): SetupTaskDialog {
            val dialog = SetupTaskDialog(context, R.style.Theme_Light_NoTitle_NoShadow_Dialog)
            val window = dialog.window
            if (window != null) {
                window.setWindowAnimations(R.style.Animation_Left_Rising)
                window.decorView.setPadding(0, 0, 0, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT
                lp.height = WindowManager.LayoutParams.MATCH_PARENT
                window.attributes = lp
                window.setGravity(Gravity.START)
            }
            val view = LayoutInflater.from(context).inflate(R.layout.std, null)
            view.findViewById<NestedScrollView>(R.id.scrollView).updateLayoutParams { width = (phoneScreenWidth * 0.46).toInt() }
            updateUI(view)
            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(canCancel)
            dialog.setCancelable(canCancel)
            return dialog
        }


        private fun updateUI(view: View) {
            val expiredDay = 1001
            val buildTime = appBuildTime
            view.findViewById<TextView>(R.id.sceneJL).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_JL) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_JL) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_JL, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneHY).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_HY) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_HY) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_HY, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }

            view.findViewById<TextView>(R.id.sceneSM).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_SM) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_SM) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_SM, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneDJT).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_JT) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_JT) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_JT, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneFW).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_FW) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_FW) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_FW, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneHM).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_HM) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_HM) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    toast("游戏->设置->画面品质改为低.不然你会卡在Boss第1阶段一直往右跑")
                    Properties.setTask(C.S_HM, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneFB).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_FB) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_FB) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_FB, null, null)
                    printLog { "风暴强烈推荐【120-125%】左右的匀速移动.不要搞急速.不然找不到水手npc.卡图" }
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneRH).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_RH) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_RH) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_RH, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneYW).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_YW) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_YW) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_YW, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneFM).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_FM) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_FM) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_FM, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }
            view.findViewById<TextView>(R.id.sceneMY).also {
                it.isVisible = (System.currentTimeMillis() - buildTime < 86400000L * expiredDay)  // x天过期
                if (!it.isVisible && Properties.scene == C.S_MY) Properties.setTask(null, null, null)
                it.setTextColor(getColor(if (Properties.scene == C.S_MY) "#ddb500" else "#44cdcdcd"))
                it.setOnClickCallback {
                    Properties.setTask(C.S_MY, null, null)
                    Permissions.notifyHomeUIUpdate()
                    updateUI(view)
                }
            }

        }
 
    }
}